package com.jdew.thkj.fragment;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jdew.thkj.R;
import com.jdew.thkj.activity.LoginActivity;
import com.jdew.thkj.activity.MainActivity;
import com.jdew.thkj.activity.TbProductDetailActivity;
import com.jdew.thkj.activity.WebViewActivity;
import com.jdew.thkj.adapter.ProductAdapter;
import com.jdew.thkj.base.BaseFragment;
import com.jdew.thkj.bean.ZhaoshangBean;
import com.jdew.thkj.dialog.CommonDialog;
import com.jdew.thkj.manager.AppConfig;
import com.jdew.thkj.manager.WrapContentLinearLayoutManager;
import com.jdew.thkj.network.RetrofitManager;
import com.jdew.thkj.network.api.RequestApi;
import com.jdew.thkj.onitemInterface.OnItemClickListener;
import com.jdew.thkj.utils.DeviceUtils;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.jdew.thkj.utils.ToastUtils;
import com.jdew.thkj.view.NetworkProgressDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yangzai on 2018/1/23 0023.
 */

public class LiveFragment extends BaseFragment implements View.OnClickListener {
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.tvRefresh)
    TextView tvRefresh;
    @Bind(R.id.swipe_target)
    RecyclerView recycleView;
    @Bind(R.id.progress)
    ProgressBar progress;
    @Bind(R.id.tv_load_prompt)
    TextView tv_load_prompt;
    @Bind(R.id.netclick)
    TextView netclick;
    @Bind(R.id.resultclick)
    TextView resultclick;
    @Bind(R.id.nonetwork)
    LinearLayout nonetwork;
    @Bind(R.id.noresult)
    LinearLayout noresult;
    @Bind(R.id.swipelayout)
    LinearLayout swipelayout;
    private MainActivity activity;
    private ProductAdapter adapter;
    private int page = 1;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private String token;
    private String mPid;
    private String ItemId;
    private NetworkProgressDialog networkProgressDialog;
    private List<ZhaoshangBean.DataBean.TbkUatmFavoritesItemGetResponseBean.ResultsBean.UatmTbkItemBean> list = new ArrayList<>();
    private List<ZhaoshangBean.DataBean.TbkUatmFavoritesItemGetResponseBean.ResultsBean.UatmTbkItemBean> list2 = new ArrayList<>();
    @Override
    protected int getContentViewId() {
        return R.layout.fragment_live;
    }

    @Override
    protected void onCreateViewT() {
        activity=(MainActivity)getActivity();
        title.setText("直播");
        adapter = new ProductAdapter(activity, list);
        title.setVisibility(View.VISIBLE);
        recycleView.setAdapter(adapter);
        recycleView.setLayoutManager(new WrapContentLinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        netclick.setOnClickListener(this);
        resultclick.setOnClickListener(this);
        adapter.setItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if(!RegexValidateUtils.isFastClick()){
                   return;
                }
                if(sp.getString("userPhone","").equals("")){
                    startActivity(new Intent(activity,LoginActivity.class));
                }else if(token.equals("")){
                    toset("授权失效,请联系客服");
                }else if(mPid.equals(AppConfig.MPID)){
                    toAdu();
                }else{
                    ItemId=list.get(position).getNum_iid();
                    toDetail();
                }
            }
        });
    }
    @Override
    public void initData() {
        superSearch();
    }

    public void superSearch(){
        if(!DeviceUtils.isNetworkAvalible(activity)){
            swipelayout.setVisibility(View.GONE);
            nonetwork.setVisibility(View.VISIBLE);
            ToastUtils.getInstance(activity).showMessage("网络连接错误请检查!");
            return;
        }else{
            swipelayout.setVisibility(View.VISIBLE);
            nonetwork.setVisibility(View.GONE);
        }
        if (null==networkProgressDialog) {
            networkProgressDialog=NetworkProgressDialog.createDialog(activity);
        }
        networkProgressDialog.show();
        RetrofitManager.getInstance(AppConfig.BASE_URL).createReq(RequestApi.class).GetDepartmentStore(page).observeOn( AndroidSchedulers.mainThread()).subscribeOn( Schedulers.io()).subscribe(new Subscriber<ZhaoshangBean>() {
            @Override
            public void onCompleted() {
                NetworkProgressDialog.DialogDismiss(networkProgressDialog);
            }

            @Override
            public void onError(Throwable e) {
                NetworkProgressDialog.DialogDismiss(networkProgressDialog);
            }
            @Override
            public void onNext(ZhaoshangBean bean) {
                NetworkProgressDialog.DialogDismiss(networkProgressDialog);
                if (bean.isSuccess()) {
                    if(null==bean.getData()){
                        tv_load_prompt.setText("无更多数据");
                        return;
                    }
                    list2 = bean.getData().getTbk_uatm_favorites_item_get_response().getResults().getUatm_tbk_item();
                    list.addAll(list2);
                    if (list2.size() < 20 && list2.size()>0) {
                    } else if (list2.size() >= 20) {
                        tv_load_prompt.setText("正在加载");
                    } else {
                        tv_load_prompt.setText("无更多数据");
                    }
                    adapter.setDataList(list);
                }
            }
        } );

    }
    public void toset(String message) {
        new CommonDialog.Builder( activity )
                .setTitle( "提示" )
                .setMessage(message)
                .setdialogPostive( "确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                    }
                }).show();
    }

    public void toDetail(){
        Bundle b = new Bundle();
        b.putSerializable("itemId", ItemId);
        startActivity(TbProductDetailActivity.class, b);
    }
    public void toAdu() {
        new CommonDialog.Builder( activity )
                .setTitle( "提示" )
                .setMessage( "设置默认推广位" )
                .setPositiveButton( "设置", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent in=new Intent(activity,WebViewActivity.class);
                        in.putExtra("type",2);
                        in.putExtra("url","http://pub.alimama.com/");
                        startActivity(in);
                    }
                }).show();
    }


    @Override
    public void onResume() {
        super.onResume();
        sp=activity.getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        token=sp.getString("token","");
        mPid=sp.getString("mpid","");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.netclick: {
            }
            break;
            case R.id.resultclick: {
            }
            break;
            default:
                break;
        }
    }
}
